package com.telecom.cms.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Administrator
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class CurrentTime {
	private static String CurrentTime;
	private static String CurrentDate;

	public static String getCurrentTime() {
		Date NowDate = new Date();
		SimpleDateFormat formatter =
			new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CurrentTime = formatter.format(NowDate);
		return CurrentTime;
	}
	public static String getCurrentTime2() {
		Date NowDate = new Date();
		SimpleDateFormat formatter =
			new SimpleDateFormat("yyyyMMddHHmmss");
		CurrentTime = formatter.format(NowDate);
		return CurrentTime;
	}

	public static String getPDate() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		Calendar c = Calendar.getInstance();   
		int d = c.get(Calendar.DAY_OF_MONTH);  
		--d;                                   
		c.set(Calendar.DAY_OF_MONTH, d);       
		String date = df.format(c.getTime());

		return date;
	}

	public static String getPDay() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		Calendar c = Calendar.getInstance();   
		int d = c.get(Calendar.DAY_OF_MONTH);  
		--d;                                   
		c.set(Calendar.DAY_OF_MONTH, d);       
		String date = df.format(c.getTime());
		try{
			java.util.Date s_date = df.parse(date);
			SimpleDateFormat formatter = new SimpleDateFormat("dd");
			return formatter.format(s_date);
		}catch(ParseException e){
			e.printStackTrace();
			return null;
		}
	}
	
	public static String getCurrentDate() {
		Date NowDate = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
		CurrentDate = formatter.format(NowDate);
		return CurrentDate;
	}
	public static String getCurrentDate2() {
		Date NowDate = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		CurrentDate = formatter.format(NowDate);
		return CurrentDate;
	}
	public static String getNowDate() {
		Date NowDate = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		CurrentDate = formatter.format(NowDate);
		return CurrentDate;
	}
	/**
	 * Returns the Compare Result
	 * if equals return "0".
	 * if before return "1".
	 * if after return "2".
	 * if error return "4".
	 * @return String
	 */
	public static String CompareDate(String SDate1, String SDate2) {
		Date DDate1, DDate2;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			DDate1 = formatter.parse(SDate1);
			DDate2 = formatter.parse(SDate2);

			if (DDate1.equals(DDate2)) {
				return "0";
			} else if (DDate1.before(DDate2)) {
				return "1";
			} else {
				return "2";
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return "4";
		}

	}
	public static String getPreMonth(String currentdate){
		Date CurrentDate=new Date();
		int EndDay=20;
		
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			
			CurrentDate=formatter.parse(currentdate);
			
			CurrentDate.setMonth(CurrentDate.getMonth()-1);
			
			return formatter.format(CurrentDate);
			
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return formatter.format(CurrentDate);
		
	}
	public static String getCurrentYear() {
		java.util.Date NowDate = new java.util.Date();

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
		return formatter.format(NowDate);
	}
	public static String getCurrentMonth() {
		java.util.Date NowDate = new java.util.Date();

		SimpleDateFormat formatter = new SimpleDateFormat("MM");
		return formatter.format(NowDate);
	}
	
	public static String getAccuPreMonth() {
		java.util.Date NowDate = new java.util.Date();
		if(NowDate.getDay()<Integer.parseInt(Constant.getStPara("PREMONTHCONDDAY")))
		    NowDate.setMonth(NowDate.getMonth()-2);
		else
			NowDate.setMonth(NowDate.getMonth()-1);
		
		SimpleDateFormat formatter = new SimpleDateFormat("MM");
		return formatter.format(NowDate);
	}
	
	public static String getAccuPreMonthAll() {
		java.util.Date NowDate = new java.util.Date();
		int oldmonth=NowDate.getMonth();
		if(NowDate.getDay()<Integer.parseInt(Constant.getStPara("PREMONTHCONDDAY")))
		    NowDate.setMonth(NowDate.getMonth()-2);
		else
			NowDate.setMonth(NowDate.getMonth()-1);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
		return formatter.format(NowDate);
	}
	
	public static String getPreMonth() {
		java.util.Date NowDate = new java.util.Date();
		NowDate.setMonth(NowDate.getMonth()-1);
		SimpleDateFormat formatter = new SimpleDateFormat("MM");
		return formatter.format(NowDate);
	}
	
	public static String getCurrentDay() {
		java.util.Date NowDate = new java.util.Date();

		SimpleDateFormat formatter = new SimpleDateFormat("dd");
		return formatter.format(NowDate);
	}
	public static int getDay() {
		java.util.Date NowDate = new java.util.Date();

//		SimpleDateFormat formatter = new SimpleDateFormat("dd");
//		return formatter.format(NowDate);
		return (new java.util.Date()).getDay();
	}
	public static String getYear(String String_date) {
		try{
			SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd");
			java.util.Date date = formatter1.parse(String_date);
			SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy");
 			return formatter2.format(date);
 		}catch(ParseException e){
			e.printStackTrace();
			return null;
		}
	}
	public static String getMonth(String String_date) {
		try{
			SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd");
			java.util.Date date = formatter1.parse(String_date);
			SimpleDateFormat formatter2 = new SimpleDateFormat("MM");
 			return formatter2.format(date);
 		}catch(ParseException e){
			e.printStackTrace();
			return null;
		}
	}
	public static String getDay(String String_date) {
		try{
			SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd");
			java.util.Date date = formatter1.parse(String_date);
			SimpleDateFormat formatter2 = new SimpleDateFormat("dd");
 			return formatter2.format(date);
 		}catch(ParseException e){
			e.printStackTrace();
			return null;
		}
	}
	
	public static long calcminutes(String starttime1,String starttime2){
		SimpleDateFormat  df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long diff = 0;
		long minutes = 0;
		try
		{
		   Date d1 = df.parse(starttime1);
		   Date d2 = df.parse(starttime2);
		   diff = d2.getTime() - d1.getTime();
		   minutes = diff / (1000*60);
		}
		catch (Exception e)
		{
		}
        return minutes;
    }

	 

}
